<?php
namespace app\bill\model;
use app\base\model\Manager;
use think\Model;


/**
 * 收支管理 Model
 */
class Trading extends Model
{

    protected $autoWriteTimestamp = 'datetime';//开启自动写入时间字段
    protected $createTime = 'add_time'; //定义创建时间字段
    protected $updateTime = 'update_time'; //定义更新时间字段

    protected $insert = ['manager_id'];



        //获取器 值得转化
    public function getBusinessIdAttr($value)
    {
        if(!$value) return '暂无';
        if(strpos($value,',')){
            $arr = explode(',' ,$value);
            $res = $this->name('role')->where('id','in',$arr)->select();
            $return  = '';
            foreach($res as $k => $v){
               $return .= $v['bname'].',';
            }
            return trim($return ,',');
        }else{
            return $this->name('business_categroy')->where('id',$value)->value('bname');
        }
        
    }

    //获取器 值得转化
    public function setBusinessIdAttr($value)
    {
        $value = is_array($value) ? join(',',$value) : $value;
        return $value;
    }
    //获取器 值得转化
    public function getTypeIdAttr($value)
    {
        if(!$value) return '暂无';
        if(strpos($value,',')){
            $arr = explode(',' ,$value);
            $res = $this->name('role')->where('id','in',$arr)->select();
            $return  = '';
            foreach($res as $k => $v){
               $return .= $v['type_name'].',';
            }
            return trim($return ,',');
        }else{
            return $this->name('trading_type')->where('id',$value)->value('type_name');
        }
        
    }

    //获取器 值得转化
    public function setTypeIdAttr($value)
    {
        $value = is_array($value) ? join(',',$value) : $value;
        return $value;
    }
    //获取器 值得转化
    public function getAccountTypeIdAttr($value)
    {
        if(!$value) return '暂无';
        if(strpos($value,',')){
            $arr = explode(',' ,$value);
            $res = $this->name('role')->where('id','in',$arr)->select();
            $return  = '';
            foreach($res as $k => $v){
               $return .= $v['account_name'].',';
            }
            return trim($return ,',');
        }else{
            return $this->name('account_type')->where('id',$value)->value('account_name');
        }
        
    }

    //获取器 值得转化
    public function setAccountTypeIdAttr($value)
    {
        $value = is_array($value) ? join(',',$value) : $value;
        return $value;
    }
    //获取器 值得转化
    public function getChannelIdAttr($value)
    {
        if(!$value) return '暂无';
        if(strpos($value,',')){
            $arr = explode(',' ,$value);
            $res = $this->name('role')->where('id','in',$arr)->select();
            $return  = '';
            foreach($res as $k => $v){
               $return .= $v['trading_name'].',';
            }
            return trim($return ,',');
        }else{
            return $this->name('trading_channel')->where('id',$value)->value('trading_name');
        }
        
    }

    //获取器 值得转化
    public function setChannelIdAttr($value)
    {
        $value = is_array($value) ? join(',',$value) : $value;
        return $value;
    }

    public function setManagerIdAttr($value){
        return session('manager.id');
    }
    public function getManagerIdAttr($value){
        $login_name = Manager::where('id' ,'=' ,$value)->value('login_name');
        return $login_name ? $login_name : '暂无';
    }
}